package com.cabbagetofu.crowd.service.impl;

import com.cabbagetofu.crowd.entity.po.MemberPO;
import com.cabbagetofu.crowd.entity.po.MemberPOExample;
import com.cabbagetofu.crowd.entity.vo.MemberVO;
import com.cabbagetofu.crowd.mapper.MemberPOMapper;
import com.cabbagetofu.crowd.service.api.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)     // 针对查询操作设置事务属性
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberPOMapper memberPOMapper;

    /**
     * 根据loginacct查询MemberPO对象
     * @param loginacct
     * @return
     */
    @Override
    public MemberPO getMemberPOByLoginAcct(String loginacct) {

        MemberPOExample memberPOExample = new MemberPOExample();
        MemberPOExample.Criteria criteria = memberPOExample.createCriteria().andLoginAcctEqualTo(loginacct);
        List<MemberPO> memberPOs = memberPOMapper.selectByExample(memberPOExample);

        if (memberPOs == null || memberPOs.size() == 0){

            return null;
        }

        return memberPOs.get(0);

    }

    /**
     * 选择性添加用户信息
     * @param memberPO
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, readOnly = false)
    @Override
    public void saveMember(MemberPO memberPO) {

        memberPOMapper.insertSelective(memberPO);

    }
}
